<template>
    <div id="background">
        <el-card class="box-card" style="background-color: rgba(64,64,64,0.35)">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
                <el-form-item style="text-align: center">
                    <span style="font-size: 30px;">欢 迎 使 用 上 海 杉 达 学 院 学 生 成 绩 管 理 系 统</span>
                </el-form-item>
                <el-form-item prop="id" style="text-align: center">
                    <span style="margin-right: 2%">用 户 名 : </span>
                    <el-input style="width: 60%;" placeholder="请输入用户名" v-model="ruleForm.id"/>
                </el-form-item>
                <el-form-item prop="password" style="text-align: center">
                    <span style="margin-right: 2%">{{'密 \u3000 码 : '}}</span>
                    <el-input style="width: 60%;" placeholder="请输入密码" v-model="ruleForm.password" show-password/>
                </el-form-item>
                 
                <el-form-item prop="type" style="text-align: center;">
                    <el-radio-group v-model="ruleForm.type">
                        <el-radio label="admin"><span>管理员</span></el-radio>
                        <el-radio label="teacher"><span>教师</span></el-radio>
                        <el-radio label="student"><span>学生</span></el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item style="text-align:center">
                    <el-button type="primary" @click="submitForm('ruleForm')">立即登录</el-button>
                    <el-button style="margin-left:60px" @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                ruleForm: {
                    id: '',
                    password:'',
                    type: '',
                },
                rules: {
                    id: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
                    ],
                    type: [
                        { required: true, message: '请选择用户类型', trigger: 'change' }
                    ]
                }
            };
        },
        created(){

        },
        methods:{
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {//校验格式
                    if (valid) {//格式校验通过
                        let path = 'http://localhost:8081/login/' + this.ruleForm.type;
                        this.$ajax.post(path,this.ruleForm).then(res=>{
                            alert(res.data.message);
                            if(res.data.status === 0){//0为登录成功 1为用户名错误 2为用户名或密码不匹配
                                sessionStorage.setItem("id",JSON.stringify(res.data.obj.id));
                                sessionStorage.setItem("name",JSON.stringify(res.data.obj.name));
                                sessionStorage.setItem("password",JSON.stringify(res.data.obj.password));
                                sessionStorage.setItem("type",JSON.stringify(res.data.obj.type));
                                this.$router.push("/" + res.data.obj.type + '/main');
                            }
                        })
                    }
                    else {
                        return false;
                    }
                });
            },
            resetForm(formName) {//重置
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style scoped>
    span{
        font-size: 20px;
        font-weight: bold;
        color: #ffffff;
    }
    .box-card{
        width: 600px;
        margin:200px auto;
    }
    #background{
        background:url('../../img/01.jpeg');
        width:100%;
        height:100%;
        position:fixed;
        background-size:100% 100%;
    }
</style>